






module Snake_Update_Data(
	
	input		clk,
	input		rst,
	
	
	input	[3:0]	direction,
	
	input[8:0]	x_addr,
	input[8:0]	y_addr,
	
	
	output reg[23:0]	odata
	
	
);

localparam Snake_width = 8;
localparam Snake_heiht = 8;

integer i,j;

wire update;


reg eated;


reg[5:0]	Snake_DataX[0:50];
reg[5:0] 	Snake_DataY[0:50];
reg[5:0]	Curr_len;



wire[5:0]	foodx;
wire[4:0]	foody;


//eat food
always@(posedge clk or negedge rst)
begin
	if(rst == 1'b0)
		eated <= 1'b0;
	else if((Snake_DataX[0] == foodx) && (Snake_DataY[0] == foody))
		eated <= 1'b1;
	else
		eated <= 1'b0;


end


//save snake current len
always@(posedge clk or negedge rst)
begin
	if(rst == 1'b0)
		Curr_len <= 'd2;
	else if(eated == 1'b1)
		Curr_len <= Curr_len + 1'b1;
	else
		Curr_len <= Curr_len;

end


always@(posedge clk or negedge rst)
begin
	if(rst == 1'b0)
	begin
		Snake_DataX[0] <= 'd0;
		Snake_DataY[0] <= 'd0;
	end
	//snake head update
	else if( direction == 4'b0001 && update == 1'b1)
	begin
		if(Snake_DataY[0] == 'd0)
		begin
			Snake_DataX[0] <= Snake_DataX[0];
			Snake_DataY[0] <= 'd33;
		end
		else
		begin
			Snake_DataX[0] <= Snake_DataX[0];
			Snake_DataY[0] <= Snake_DataY[0] - 1'b1;
		end
		
	end
	else if(direction == 4'b0010 && update == 1'b1)
	begin
		if(Snake_DataY[0] == 'd33)
		begin	
			Snake_DataX[0] <= Snake_DataX[0];
			Snake_DataY[0] <= 'd0;
		end
		else
		begin
			Snake_DataX[0] <= Snake_DataX[0];
			Snake_DataY[0] <= Snake_DataY[0] + 1'b1;
		end
		
	end
	else if(direction == 4'b0100 && update == 1'b1)
	begin
		if(Snake_DataX[0] == 'd0)
		begin
			Snake_DataX[0] <= 'd59;
			Snake_DataY[0] <= Snake_DataY[0];
		end
		else begin
			Snake_DataX[0] <= Snake_DataX[0] - 1'b1;
			Snake_DataY[0] <= Snake_DataY[0];
		end
		
	end
	else if(direction == 4'b1000 && update == 1'b1)
	begin
		if(Snake_DataX[0] == 'd59)
		begin
			Snake_DataX[0] <= 'd0;
			Snake_DataY[0] <= Snake_DataY[0];
		end
		else
		begin
			Snake_DataX[0] <= Snake_DataX[0] + 1'b1;
			Snake_DataY[0] <= Snake_DataY[0];
		end
		
	end
	else
	begin

		Snake_DataX[0] <= Snake_DataX[0];
		Snake_DataY[0] <= Snake_DataY[0];	
	end
	
	
end


always@(posedge clk or negedge rst)
begin
	if(rst == 1'b0)
	begin
	for(i=1; i<50; i = i + 1)
	begin
		Snake_DataX[i] <= 'd0;
		Snake_DataY[i] <= 'd0;
	end
	end
	
	else if(update == 1'b1)
	begin
	for(i=1; i<50; i = i + 1)
	begin
		Snake_DataX[i] <= Snake_DataX[i-1];
		Snake_DataY[i] <= Snake_DataY[i-1];
	end
	end
	

end

always@(posedge clk or negedge rst)
begin
	if(rst == 1'b0)
		odata <= 'd0;
	else 
	begin
		
		if((x_addr >= Snake_DataX[0]*'d8 && x_addr <= (Snake_DataX[0] + 1'b1) * 'd8) && (y_addr >= Snake_DataY[0]*'d8 && y_addr <= (Snake_DataY[0] + 1'b1) * 'd8) && Curr_len >= 1)				
			odata <= 24'hFFE23;
		else if((x_addr >= Snake_DataX[1]*'d8 && x_addr <= (Snake_DataX[1] + 1'b1) * 'd8) && (y_addr >= Snake_DataY[1]*'d8 && y_addr <= (Snake_DataY[1] + 1'b1) * 'd8) && Curr_len >= 2)				
			odata <= 24'hFF1010;
		else if((x_addr >= Snake_DataX[2]*'d8 && x_addr <= (Snake_DataX[2] + 1'b1) * 'd8) && (y_addr >= Snake_DataY[2]*'d8 && y_addr <= (Snake_DataY[2] + 1'b1) * 'd8) && Curr_len >= 3)				
			odata <= 24'hFF2020;
		else if((x_addr >= Snake_DataX[3]*'d8 && x_addr <= (Snake_DataX[3] + 1'b1) * 'd8) && (y_addr >= Snake_DataY[3]*'d8 && y_addr <= (Snake_DataY[3] + 1'b1) * 'd8) && Curr_len >= 4)				
			odata <= 24'hFF3030;
		else if((x_addr >= Snake_DataX[4]*'d8 && x_addr <= (Snake_DataX[4] + 1'b1) * 'd8) && (y_addr >= Snake_DataY[4]*'d8 && y_addr <= (Snake_DataY[4] + 1'b1) * 'd8) && Curr_len >= 5)				
			odata <= 24'hFF4040;
		else if((x_addr >= Snake_DataX[5]*'d8 && x_addr <= (Snake_DataX[5] + 1'b1) * 'd8) && (y_addr >= Snake_DataY[5]*'d8 && y_addr <= (Snake_DataY[5] + 1'b1) * 'd8) && Curr_len >= 6)				
			odata <= 24'hFF5050;
		else if((x_addr >= Snake_DataX[6]*'d8 && x_addr <= (Snake_DataX[6] + 1'b1) * 'd8) && (y_addr >= Snake_DataY[6]*'d8 && y_addr <= (Snake_DataY[6] + 1'b1) * 'd8) && Curr_len >= 7)				
			odata <= 24'hFF6060;
		else if((x_addr >= Snake_DataX[7]*'d8 && x_addr <= (Snake_DataX[7] + 1'b1) * 'd8) && (y_addr >= Snake_DataY[7]*'d8 && y_addr <= (Snake_DataY[7] + 1'b1) * 'd8) && Curr_len >= 8)				
			odata <= 24'hFF7070;
		else if((x_addr >= Snake_DataX[8]*'d8 && x_addr <= (Snake_DataX[8] + 1'b1) * 'd8) && (y_addr >= Snake_DataY[8]*'d8 && y_addr <= (Snake_DataY[8] + 1'b1) * 'd8) && Curr_len >= 9)				
			odata <= 24'hFF8080;
		else if((x_addr >= Snake_DataX[9]*'d8 && x_addr <= (Snake_DataX[9] + 1'b1) * 'd8) && (y_addr >= Snake_DataY[9]*'d8 && y_addr <= (Snake_DataY[9] + 1'b1) * 'd8) && Curr_len >= 10)				
			odata <= 24'hFF9090;
		else if((x_addr >= Snake_DataX[10]*'d8 && x_addr <= (Snake_DataX[10] + 1'b1) * 'd8) && (y_addr >= Snake_DataY[10]*'d8 && y_addr <= (Snake_DataY[10] + 1'b1) * 'd8) && Curr_len >= 11)				
			odata <= 24'hFF1000;
		else if((x_addr >= Snake_DataX[11]*'d8 && x_addr <= (Snake_DataX[11] + 1'b1) * 'd8) && (y_addr >= Snake_DataY[11]*'d8 && y_addr <= (Snake_DataY[11] + 1'b1) * 'd8) && Curr_len >= 12)				
			odata <= 24'hFF2000;
		else if((x_addr >= Snake_DataX[12]*'d8 && x_addr <= (Snake_DataX[12] + 1'b1) * 'd8) && (y_addr >= Snake_DataY[12]*'d8 && y_addr <= (Snake_DataY[12] + 1'b1) * 'd8) && Curr_len >= 13)				
			odata <= 24'hFF3000;
		else if((x_addr >= Snake_DataX[13]*'d8 && x_addr <= (Snake_DataX[13] + 1'b1) * 'd8) && (y_addr >= Snake_DataY[13]*'d8 && y_addr <= (Snake_DataY[13] + 1'b1) * 'd8) && Curr_len >= 14)				
			odata <= 24'hFF4000;
		else if((x_addr >= Snake_DataX[14]*'d8 && x_addr <= (Snake_DataX[14] + 1'b1) * 'd8) && (y_addr >= Snake_DataY[14]*'d8 && y_addr <= (Snake_DataY[14] + 1'b1) * 'd8) && Curr_len >= 15)				
			odata <= 24'hFF5000;
		else if((x_addr >= Snake_DataX[15]*'d8 && x_addr <= (Snake_DataX[15] + 1'b1) * 'd8) && (y_addr >= Snake_DataY[15]*'d8 && y_addr <= (Snake_DataY[15] + 1'b1) * 'd8) && Curr_len >= 16)				
			odata <= 24'hFF6000;
			
			
		else if((x_addr >= foodx*'d8 && x_addr <= (foodx + 1'b1) * 'd8) && (y_addr >= foody*'d8 && y_addr <= (foody + 1'b1) * 'd8))				
			odata <= 24'hFF00FF;
		else
			odata <= 24'h000000;
		
	end

end




//LFSR Gen food x y
FOOD_Gen   FOOD_Gen_HP(

	.clk		(clk),
	.rst		(rst),
	
	.seedx      ('d2),
	.seedy	('d5),
	
	.eated	(eated),
	
	.foodx	(foodx),
	.foody	(foody)

);






//control snake move speed
TIME   TIME_HP(
	
	.clk_50M  (clk),
	.rst      (rst),
	.clk_1s   (update)
);





endmodule 